package com.darwin.darwinUnit
{
	import flash.utils.getQualifiedClassName;

	/**
	 * 测试工具类，TestUtils通过simpleMethod提供简单的测试方法
	 * @author lipi
	 */
	public class TestUtils
	{
		public function TestUtils(testCase:DarwinTestCase)
		{
			_case = testCase;
		}
		
		private var _case:DarwinTestCase;
		
		public function simpleMethod(instance:Object,methodName:String,arg:* = null,result:* = null):*
		{
			if(arg != null)
			{
				if(!(arg is Array))
				{
					arg = [arg];
				}
			}
			var $result:Object;
			var $testResult:TestResult = new TestResult();
			$testResult.className = getQualifiedClassName(instance);
			$testResult.methodName = methodName;
			
			var $method:Function = instance[methodName];
			if($method == null)
			{
				$testResult.succeed = false;
				$testResult.type = TestResult.NOT_METHOD;
				$testResult.desc = "你要测试的方法不存在";
				$testResult.arg = arg;
				$testResult.result = "未执行";
				_case.pushResult($testResult);
				return "没正确执行";
			}else{
				$result = $method.apply(instance,arg);
				if($result != result)
				{
					$testResult.succeed = false;
					$testResult.type = TestResult.ERROR_RESULT;
					$testResult.desc = "返回的结果不正确";
					$testResult.arg = arg;
					$testResult.result = $result;
					_case.pushResult($testResult);
					return $result;
				}else{
					$testResult.succeed = true;
					$testResult.type = TestResult.OK;
					$testResult.desc = "";
					$testResult.arg = arg;
					$testResult.result = $result;
					_case.pushResult($testResult);
					return $result;
				}
			}
			return "未知";
		}
		
		
		private function arrayToString(arr:Array):String
		{
			if(arr == null) return "(NULL)";
			var $str:String = "";
			for(var i:int = 0;i<arr.length;i++)
			{
				$str+= arr[i].toString();
				if(i<arr.length - 1)
				{
					$str += "，";
				}
			}
			$str += "";
			return $str;
		}
	}
}